<?php
class Administrator_UserrequestController extends Zend_Controller_Action
{
	public function init() {
		
	}
	
	public function indexAction(){
		$objUserrequest = new HT_Model_administrator_models_userrequest();
		$do = @$this->_request->getParam('do');
		$request_id = (int)$this->_request->getParam('id');
		if($do == 'delete' && $request_id >0){
			$this->deleteUserrequest($request_id);
		}elseif($do == 'list'){
			$this->getListUserrequest();
		}else{
			$keyword = $this->_request->getParam('keyword');
			$this->view->keyword 	= $keyword;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/userrequest/index.js');
	}
	
	public function updateAction(){
		$auth    = Zend_Auth::getInstance();
		$user    = $auth->getStorage()->read();
		$objUserrequest = new HT_Model_administrator_models_userrequest();
		$do 		 = @$this->_request->getParam('do');
		$request_id = (int)$this->_request->getParam('id');
		$status 	= (int)$this->_request->getParam('status');
		$request_status = $this->_request->getParam('request_status');
		if($request_status == 'on'){
			$request_status = 1;
		}else{
			$request_status = 0;
		}
		//echo $do; die();
		if($do == 'submit'){
			$data = array();
			$data['subject'] 			= $this->_request->getParam('subject');
			$data['content'] 			= $this->_request->getParam('content');
			$data['request_status'] 	= $request_status;
			if($request_id >0){
				$data['admin_id'] 		= $user->wishid;
				$data['process_date'] 	= date('Y-m-d H:i:s');
				$objUserrequest->update($data, 'request_id='.(int)$request_id);
			}else{
				$data['request_date'] 	= date('Y-m-d H:i:s');
				$data['ip_request'] 	= $_SERVER['REMOTE_ADDR'];
				$request_id 			= $objUserrequest->addData($data);
			}
			//$this->_redirect(WEB_PATH.'/administrator/userrequest/update?status=1&id='.$request_id);
			$this->_redirect(WEB_PATH.'/administrator/userrequest');
		}elseif($request_id >0){
			$this->view->userrequest = $objUserrequest->getUserrequest($request_id);
		}
		$this->view->id = $request_id;
		$this->view->status 	 = $status;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/userrequest/update.js');
	}

	function deleteUserrequest($request_id){
		$objUserrequest = new HT_Model_administrator_models_userrequest();
		echo $objUserrequest->delete("request_id=".(int)$request_id);die();
	}

	function getListUserrequest(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objUserrequest 	= new HT_Model_administrator_models_userrequest();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
		$totalRecord = $objUserrequest->getListUserrequest_nb(array('keyword'=>$keyword));
		$listUserrequest = $objUserrequest->getListUserrequest($start,$size,array('keyword'=>$keyword));
		$paging = trim($objUtil->paging($page, $size, $totalRecord));

		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
			$ajaxData .= '<tr>';
				$ajaxData .= '<th width="15">STT</th>';
				$ajaxData .= '<th width="200">Chủ đề</th>';
				$ajaxData .= '<th width="500">Yêu cầu</th>';
				$ajaxData .= '<th width="100">IP request</th>';
				$ajaxData .= '<th width="100">Ngày yêu cầu</th>';
				$ajaxData .= '<th width="100">Ngày xử lý</th>';
				$ajaxData .= '<th width="50">Status</th>';
				$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
			$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
		
		$i=0;
		foreach($listUserrequest as $userrequest){
			$ip_request = null;
			if(isset($userrequest['ip_request'])){
				$ip_request = $userrequest['ip_request'];
			}
			$request_date = null;
			if(isset($userrequest['request_date'])){
				$request_date = $objUtil->normalDate($userrequest['request_date']);
			}
			$process_date = null;
			if(isset($userrequest['process_date'])){
				$process_date =	$objUtil->normalDate($userrequest['process_date']);
			}
			$request_status = null;
			if(isset($userrequest['request_status']) && $userrequest['request_status'] == 1){
				$prequest_status =	"<b>OK</b>";
			}
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$userrequest['request_id'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td><a href="'.WEB_PATH.'/administrator/userrequest/update/?id='.$userrequest['request_id'].'">'.$objUtil->tooltipString($userrequest['subject'],200).'</a></td>';
			$ajaxData .= '<td>'.$userrequest['content'].'</td>';
			$ajaxData .= '<td>'.$ip_request.'</td>';
			$ajaxData .= '<td>'.$request_date.'</td>';
			$ajaxData .= '<td>'.$process_date.'</td>';
			$ajaxData .= '<td>'.$request_status.'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteUserrequest('.$userrequest['request_id'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/userrequest/update/?id='.$userrequest['request_id'].'">Sửa</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		echo $ajaxData; die();
	}
}
